﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data.SqlClient;
using System.Data;

namespace SysRoutesWS
{
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class Service1 : System.Web.Services.WebService
    {
        static string strConn =
        "Data Source= JHOSEPH-PC\\SQLEXPRESS;" +
        "Initial Catalog=SysRoutes;User ID=sa;Password=senha";
        SqlConnection conn = new SqlConnection(strConn);

        [WebMethod]
        public List<Parada> ListarParadas()                         // Listar os nomes das paradas.
        {
            List<Parada> list = new List<Parada>();
            SqlCommand cmd = new SqlCommand("ListarParadas", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            conn.Open();

            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                Parada p = new Parada();
                p.nome = dr[0].ToString();
                p.id_parada = Convert.ToInt32(dr[1].ToString());
                list.Add(p);
            }
            conn.Close();

            return list;
        }

        [WebMethod]
        public List<Linha> ListarLinha()
        {
            List<Linha> list = new List<Linha>();
            SqlCommand cmd = new SqlCommand("ListarLinha", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            conn.Open();

            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                Linha l = new Linha();
                l.id_linha = Convert.ToInt32(dr[0].ToString());
                l.numero = dr[1].ToString();
                list.Add(l);
            }
            conn.Close();

            return list;
        }

        [WebMethod]
        public List<Linha> LinhaSelect(int id)                      // Retorna as Linhas de uma Parada.
        {
            List<Linha> list = new List<Linha>();
            SqlCommand cmd = new SqlCommand("LinhaSelect", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@idparada", id);
            conn.Open();

            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                Linha l = new Linha();
                l.numero = dr[0].ToString();
                l.id_linha = Convert.ToInt32(dr[1].ToString());
                list.Add(l);
            }
            conn.Close();
            return list;
        }

        [WebMethod]
        public List<Parada> ParadaSelect(int id)                    // Retorna as Paradas de uma Linha.
        {
            List<Parada> list = new List<Parada>();
            SqlCommand cmd = new SqlCommand("ParadaSelect", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@idlinha", id);
            conn.Open();

            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                Parada p = new Parada();
                p.nome = dr[0].ToString();
                p.id_parada = Convert.ToInt32(dr[1].ToString());
                list.Add(p);
            }
            conn.Close();
            return list;
        }

        [WebMethod]
        public List<Linha> BuscarLinha(int p1, int p2)              // Retorna as linhas que passam na Parada p1 e na Parada p2
        {
            List<Linha> l1 = new List<Linha>();
            List<Linha> l2 = new List<Linha>();
            List<Linha> list = new List<Linha>();

            l1 = LinhaSelect(p1);
            l2 = LinhaSelect(p2);

            foreach (Linha lx in l1)
            {
                foreach (Linha ly in l2)
                {
                    if (lx.numero.Equals(ly.numero))
                    {
                        list.Add(lx);
                    }
                }
            }

            return list;
        }

        /*
        [WebMethod]
        public int[] ParadaIDSelect(String p1, String p2)
        {
            int[] x = new int[2];
            int count = 0;
            SqlCommand cmd = new SqlCommand("ParadaIDSelect", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@nome1", p1);
            cmd.Parameters.AddWithValue("@nome2", p2);
            conn.Open();

            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                x[count++] = Convert.ToInt32(dr[0].ToString());
            }
            conn.Close();

            return x;
        }
        */

    }
}